import java.net.*;
import java.io.*;

//import Connection;

public class Client
{
    Connection connection;
    MCMASServerData currentData;
    RunMCMAS runMCMAS;

    public class RunMCMAS extends Thread
    {
        public void run()
        {
            try
            {
                System.out.println("Processing: " + currentData.seedState); 
                MCMASClientData data = new MCMASClientData();
                MCMASParser.MCMASSlaveParser(currentData, data);
                data.seedState = currentData.seedState;
                //Process p = Runtime.getRuntime().exec("./test.sh");
                //p.waitFor();
                //if (Integer.parseInt(data.seedState) == 50)
                //    data.cexFound = true;
                connection.objOut.writeObject(data);
            }
            catch (Exception e)
            {
                //System.out.println("Dying");
            }
        }
    }

    public Client(String machine)
    {
        try 
        {
            connection = new Connection(new Socket(machine, 1337));
            MCMASServerData serverData;
            System.out.println("Connected");

            while ((serverData = (MCMASServerData) connection.objIn.readObject()) != null)
            {
                currentData = serverData;
                //runMCMAS.start();
                runMCMAS = new RunMCMAS();
                runMCMAS.start();
            }
        }
        catch (Exception e)
        {
            runMCMAS = null;
            System.out.println("Server closed, closing ...");
        }
    }

    public static void main(String[] args)
    {
        new Client(args[0]);
        System.exit(0);
    }
}
       
